package com.shanxi.cz.module.biz.service.ammeterrecord;

import com.google.common.collect.Maps;
import com.shanxi.cz.framework.common.pojo.PageResult;
import com.shanxi.cz.framework.common.util.object.BeanUtils;
import com.shanxi.cz.module.biz.controller.admin.ammeterrecord.vo.AmmeterRecordPageReqVO;
import com.shanxi.cz.module.biz.controller.admin.ammeterrecord.vo.AmmeterRecordRespVO;
import com.shanxi.cz.module.biz.controller.admin.ammeterrecord.vo.AmmeterRecordSaveReqVO;
import com.shanxi.cz.module.biz.dal.dataobject.ammeterrecord.AmmeterRecordDO;
import com.shanxi.cz.module.biz.dal.mysql.ammeterrecord.AmmeterRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.shanxi.cz.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.shanxi.cz.module.biz.enums.ErrorCodeConstants.AMMETER_RECORD_NOT_EXISTS;

/**
 * 电表数据记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class AmmeterRecordServiceImpl implements AmmeterRecordService {

    @Resource
    private AmmeterRecordMapper ammeterRecordMapper;

    @Override
    public Integer createAmmeterRecord(AmmeterRecordSaveReqVO createReqVO) {
        // 插入
        AmmeterRecordDO ammeterRecord = BeanUtils.toBean(createReqVO, AmmeterRecordDO.class);
        ammeterRecordMapper.insert(ammeterRecord);

        // 返回
        return ammeterRecord.getId();
    }

    @Override
    public void createAmmeterRecordBatch(List<AmmeterRecordSaveReqVO> createReqVOs) {
        List<AmmeterRecordDO> ammeterRecords = BeanUtils.toBean(createReqVOs, AmmeterRecordDO.class);
        ammeterRecordMapper.insertBatch(ammeterRecords);
    }

    @Override
    public void updateAmmeterRecord(AmmeterRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateAmmeterRecordExists(updateReqVO.getId());
        // 更新
        AmmeterRecordDO updateObj = BeanUtils.toBean(updateReqVO, AmmeterRecordDO.class);
        ammeterRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteAmmeterRecord(Integer id) {
        // 校验存在
        validateAmmeterRecordExists(id);
        // 删除
        ammeterRecordMapper.deleteById(id);
    }

    @Override
    public void deleteAmmeterRecordListByIds(List<Integer> ids) {
        // 删除
        ammeterRecordMapper.deleteByIds(ids);
    }


    private void validateAmmeterRecordExists(Integer id) {
        if (ammeterRecordMapper.selectById(id) == null) {
            throw exception(AMMETER_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public AmmeterRecordDO getAmmeterRecord(Integer id) {
        return ammeterRecordMapper.selectById(id);
    }

    @Override
    public PageResult<AmmeterRecordDO> getAmmeterRecordPage(AmmeterRecordPageReqVO pageReqVO) {
        return ammeterRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AmmeterRecordDO> selectByParams(AmmeterRecordPageReqVO pageReqVO) {
        return ammeterRecordMapper.selectByParams(pageReqVO);
    }

    @Override
    public Map<String, AmmeterRecordRespVO> listByDate(AmmeterRecordPageReqVO pageReqVO) {
        Map<String, AmmeterRecordRespVO> dataMap = Maps.newHashMap();

        List<AmmeterRecordDO> ammeterRecordList = ammeterRecordMapper.selectByParams(pageReqVO);

        Map<String, AmmeterRecordDO> recordMap = ammeterRecordList.stream().collect(Collectors.toMap(AmmeterRecordDO::getRecordTime, Function.identity()));

        int n = 24 * 12; // 24小时拆分成24个时间段
        LocalTime startTime = LocalTime.MIDNIGHT; // 一天的开始
//        Duration oneHour = Duration.ofHours(1);
        Duration fiveMinutes = Duration.ofMinutes(5);

        for (int i = 0; i < n; i++) {
            String recordTime = startTime.toString();
            AmmeterRecordDO ammeterRecordDO = recordMap.get(recordTime);
            AmmeterRecordRespVO ammeterRecordRespVO = BeanUtils.toBean(ammeterRecordDO, AmmeterRecordRespVO.class);
            dataMap.put(recordTime, ammeterRecordRespVO);
            startTime = startTime.plus(fiveMinutes);
        }
        return dataMap;
    }

}